package com.yuexun.dhdoctor.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/8/31 0031.
 */

public class MeetingRoomDialog {
    private Context context;
    private Dialog dialog;
    private Display display;

    private TextView tv_meetingroom_name;
    private TextView tv_meetingroom_theme;
    private TextView tv_meetingroom_type;
    private TextView tv_meetingroom_time;
    private TextView tv_meetingroom_status;
    private TextView tv_meetingroom_person;
    private Button sure;


    public MeetingRoomDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public MeetingRoomDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_meetingroom, null);


        tv_meetingroom_name = (TextView) view.findViewById(R.id.tv_meetingroom_name);
        tv_meetingroom_theme = (TextView) view.findViewById(R.id.tv_meetingroom_theme);
        tv_meetingroom_type = (TextView) view.findViewById(R.id.tv_meetingroom_type);
        tv_meetingroom_time = (TextView) view.findViewById(R.id.tv_meetingroom_time);
        tv_meetingroom_status = (TextView) view.findViewById(R.id.tv_meetingroom_status);
        tv_meetingroom_person = (TextView) view.findViewById(R.id.tv_meetingroom_person);
        sure = (Button) view.findViewById(R.id.sure);


        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.loadingDialogStyle);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    public MeetingRoomDialog setContent(String name, String theme, String type, String startDate, String endDate, String status, String person) {
        tv_meetingroom_name.setText(name);
        if(theme!=null) {
            tv_meetingroom_theme.setText(theme);
        }
        else {
            tv_meetingroom_theme.setText("");
        }
        tv_meetingroom_type.setText(type);
        try {
            Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDate);
            Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                if (dateFormat.parse(startDate).equals(dateFormat.parse(endDate))) {
                    tv_meetingroom_time.setText(new SimpleDateFormat("MM-dd HH:mm").format(start) + " - " + new SimpleDateFormat("HH:mm").format(end));
                } else {
                    tv_meetingroom_time.setText(new SimpleDateFormat("MM-dd HH:mm").format(start) + " - " + new SimpleDateFormat("MM-dd HH:mm").format(end));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (status.equals("1")) {
            tv_meetingroom_status.setText("申请中");
        } else if (status.equals("2")) {
            tv_meetingroom_status.setText("已完成");
        }
        tv_meetingroom_person.setText(person);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

}
